package com.wufei.tastefor.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by wufei on 2015/9/30.
 */
public class IsFirstUtils {

    /**
     * 判断是否是第一次运行
     */

    public static boolean isFirst(Context context) {
        int versionCode;
        boolean first;
        SharedPreferences sp = context.getSharedPreferences(AppConfig.APP_SP_ISFIRST, Context.MODE_PRIVATE);
        first = sp.getBoolean(AppConfig.APP_ISFIRST_KEY, true);
        versionCode = sp.getInt(AppConfig.APP_VERSIONCODE_KEY, 1);
        if (getVersion(context) != versionCode || first) {
            sp.edit().putBoolean(AppConfig.APP_ISFIRST_KEY, false).commit();
            sp.edit().putInt(AppConfig.APP_VERSIONCODE_KEY, getVersion(context)).commit();
            sp.edit().putBoolean(AppConfig.APP_IS_LOGIN_KEY,false).commit();
        }
        return first;
    }

    //判断是否登录
    public static boolean isLogin(Context context){
        boolean login;
        SharedPreferences sp = context.getSharedPreferences(AppConfig.APP_IS_LOGIN, Context.MODE_PRIVATE);
        login = sp.getBoolean(AppConfig.APP_IS_LOGIN_KEY,false);
        return login;
    }

    /**
     * 获得程序版本号
     */
    public static int getVersion(Context context) {
        int versionCode = 1;
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
            versionCode = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }
}
